//
//  EGTabBarController.m
//  Orchard
//
//  Created by ios001 on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "EGTabBarController.h"
#import "EGSupermarketViewController.h"
#import "EGShoppingCartController.h"
#import "EGHomeViewController.h"
#import "EGProfileController.h"


@interface EGTabBarController ()<UITabBarControllerDelegate>

@end

@implementation EGTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupChildVc];
    self.delegate = self;
  
}



#pragma mark - 设置子控制器
- (void)setupChildVc{
    
    UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:[[EGHomeViewController alloc] init]];
    
    [self setupViewController:homeNav title:@"首页" imgNormal:@"v2_home" imgSelected:@"v2_home_r"];
    
    UINavigationController *messageNav = [[UINavigationController alloc] initWithRootViewController:[[EGSupermarketViewController alloc] init]];
    
    [self setupViewController:messageNav title:@"超市" imgNormal:@"v2_order" imgSelected:@"v2_order_r"];
    
    UINavigationController *discoverNav = [[UINavigationController alloc] initWithRootViewController:[[EGShoppingCartController alloc] init]];
    [self setupViewController:discoverNav title:@"购物车" imgNormal:@"shopCart" imgSelected:@"shopCart_r"];
    
    UINavigationController *profileNav = [[UINavigationController alloc] initWithRootViewController:[[EGProfileController alloc] init]];
    
    [self setupViewController:profileNav title:@"我" imgNormal:@"v2_my" imgSelected:@"v2_my_r"];
    
    self.viewControllers = @[homeNav, messageNav, discoverNav, profileNav];
}

#pragma mark - 设置tabBarItem
- (void)setupViewController:(UINavigationController *)nav title:(NSString *)title imgNormal:(NSString *)imgNormal imgSelected:(NSString *)imgSelected{
    
    nav.tabBarItem.title = title;
    nav.title = title;
    [nav.tabBarItem setImage:[UIImage imageNamed:imgNormal]];
    [nav.tabBarItem setSelectedImage:[UIImage imageNamed:imgSelected]];
    
    [nav.navigationBar setBarTintColor:ThemeColor];
    
    //设置导航栏中间 ---> 字体颜色
    [nav.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    //设置导航栏返回按钮的颜色
    nav.navigationBar.tintColor = [UIColor blackColor];
}

// 拦截push控制器
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    // 取出当前控制器
    NSInteger currentIndex = [self.childViewControllers indexOfObject:viewController];
    // 判断是否是要拦截的控制器
    if (currentIndex == 2) {
        // 进行modal操作
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:[EGShoppingCartController new]];
        
        [self presentViewController:nav animated:YES completion:nil];
        
        return NO;
    }
    return YES;
}



#pragma mark - 懒加载控件

@end
